<?php
namespace app\api\model;
use app\entries\model\Entries;

/**
 *
 */
class Api extends Entries
{
    //获取茶的种类
    function getTeaCase()
    {
        $res = db('teacase')->select();
        return $res;
    }


    //获取某茶类所有的茶产区
    function getTeaArea($id)
    {
        $condition = ['teacase_id'=>$id, 'display'=>1];
        $retval = db('case_area')->field('teaarea_id')->where($condition)->select();
        // $retval = db('case_area')->field('teaarea_id')->where('teacase_id', $id)->select();
        if(!$retval){
            return 0;
        }

        $teaarea_ids = $this->teaarea_ids();//产区id和产区的关联数组
        foreach ($retval as &$ret) {
            $ret['teacase_id'] = $id;
            $ret['area'] = $teaarea_ids[$ret['teaarea_id']];
        }
        return $retval;
    }


    //获取榜单
    function getBangDan($teacase_id, $teaarea_id, $bangdan_id=1)
    {
        if($teaarea_id!=0){
           //查询条件
            $data = [
                'bangdan_id' => $bangdan_id,
                'teacase_id' => $teacase_id,
                'teaarea_id' => $teaarea_id
            ];
        }else{
           //查询条件
            $data = [
                'bangdan_id' => $bangdan_id,
                'teacase_id' => $teacase_id,
            ];
        }

        

        //1为匠人榜，2为品牌榜，3为珍品榜
        if($bangdan_id==1) {
            $res = db('jiangren')->field('id, bangdan_id, teacase_id, teaarea_id, name, score ')->where($data)->order("score desc")->limit(10)->select();
            return $res;
        }else if ($bangdan_id==2) {
            $res = db('pinpai')->field('id, bangdan_id, teacase_id, teaarea_id, name, score')->where($data)->order("score desc")->limit(10)->select();
            return $res;
        }else if($bangdan_id==3){
            $res = db('zhenpin')->field('id, bangdan_id, teacase_id, teaarea_id, name, score')->where($data)->order("score desc")->limit(10)->select();
            return $res;
        }else{
            return 0;
        }
    }


    //获取匠人或珍品或品牌的详细信息
    function getDetail($id, $bangdan_id){
        //1为匠人榜，2为品牌榜，3为珍品榜
        if($bangdan_id==1) {
            db('jiangren')->where('id', $id)->setInc('click');
            $res = db('jiangren')->where('id', $id)->find();
            return $res;
        }elseif ($bangdan_id==2) {
            db('pinpai')->where('id', $id)->setInc('click');
            $res = db('pinpai')->where('id', $id)->find();
            return $res;

        }elseif($bangdan_id==3){
            db('zhenpin')->where('id', $id)->setInc('click');
            $res = db('zhenpin')->where('id', $id)->find();
            return $res;
        }else{
            return 0;
        }
    }


    //获取版权说明
    function findcopyright(){
        return db('copyright')->field('content')->order('id desc')->limit(1)->find();
    }


}




 ?>